//
// Created by benxb on 2021/11/11.
//

#ifndef BENXDB_COLUMN_STORAGE_H
#define BENXDB_COLUMN_STORAGE_H

#include <string>
#include <fstream>

namespace benxdb {

class ColumnStorage {
public:
    /**
     * Create a new column storage that writes to the specified database file
     * @param db_file the file name of the database file
     */
    explicit ColumnStorage(const std::string &db_file);

    ~ColumnStorage() = default;

    /**
     * Shut down the disk manager and close all the file resources
     */
    void ShutDown();

    /**
     * Write page to data_ file
     * @param page_data
     * @param page_size
     * @return
     */
    int64_t WritePage(const char *page_data, int64_t page_size);

    /**
     * Read page data_ from data_ file
     * @param page_id
     * @param page_data
     */
    void ReadPage(int64_t page_id, std::string& page_data);


private:
    // stream to write db file
    std::fstream db_io_;
    std::string file_name_;
    int num_flushes_{};
    int num_writes_{};

    /**
     * Write data_ of page into database file
     * @param page_offset
     * @param page_data
     * @param page_size
     */
    void AppendPageData(const char *page_data, int64_t page_size);

    /**
     * Read page data_ from a database file
     * @param page_offset
     * @param page_size
     * @param page_data
     */
    void ReadPageData(int64_t page_offset, int64_t page_size, std::string& page_data);
};

}


#endif //BENXDB_COLUMN_STORAGE_H
